package cn.lg.soar.cms.biz.modules.other.controller;

import cn.lg.soar.cms.api.constant.ModuleConst;
import cn.lg.soar.cms.biz.modules.other.entity.Block;
import cn.lg.soar.cms.biz.modules.other.model.BlockItemVo;
import cn.lg.soar.cms.biz.modules.other.model.BlockVO;
import cn.lg.soar.cms.biz.modules.other.service.BlockService;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 页面元素位置
 * @author luguoxiang
 * @since 2025-02-23
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Tag(name = "页面元素位置")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/other/block")
public class BlockController extends BaseController<Block, BlockService> {


    @Override
    protected Serializable getId(Block entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "页面位置";

    @Log(value = "新增" + SUBJECT, type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated BlockVO data) {
        return HttpResult.data(service.create(data));
    }

    @Log(value = "修改" + SUBJECT, type = LogTypeEnum.UPDATE)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated BlockVO data) {
        service.update(data);
        return HttpResult.success();
    }

    @Log(value = "删除" + SUBJECT, type = LogTypeEnum.REMOVE)
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        service.remove(ids);
        return HttpResult.success();
    }

    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<BlockVO> detail(@RequestParam(name = "id") @Id Long id) {
        return HttpResult.data(service.detail(id));
    }

    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<Block>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @Log(value = "导出" + SUBJECT, type = LogTypeEnum.EXPORT)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(@RequestParam(name = "filename", required = false) String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入" + SUBJECT, type = LogTypeEnum.IMPORT)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @Operation(summary = "获取位置内容")
    @GetMapping("contents")
    public HttpResult<List<BlockItemVo>> contents(@RequestParam(name = "code") String code, @RequestParam(name = "pageSize") Integer pageSize) {
        return HttpResult.data(service.contents(code, pageSize));
    }

}